package com.shs.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.sys.common.DataGridView;
import com.shs.sys.common.ResultObj;
import com.shs.sys.entity.Loginfo;
import com.shs.sys.service.LoginfoService;
import com.shs.sys.vo.LoginfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  日志前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-09
 */
@RestController
@RequestMapping("loginfo")
public class LoginfoController {

    @Autowired
    private LoginfoService loginfoService;

    /**
     * 查询日志
     * @return
     */
    @RequestMapping("loadAllLoginfo")
    public DataGridView loadAllLoginfo(LoginfoVo vo){
        Page<Loginfo> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Loginfo> wrapper = new QueryWrapper();
        wrapper.like(StringUtils.isNotBlank(vo.getLoginname()),"loginname",vo.getLoginname());
        wrapper.like(StringUtils.isNotBlank(vo.getLoginip()),"loginip",vo.getLoginip());
        //大于
        wrapper.ge(vo.getStartTime()!=null,"logintime",vo.getStartTime());
        //小于
        wrapper.le(vo.getEndTime()!=null,"logintime",vo.getEndTime());
        wrapper.orderByDesc("logintime");
        loginfoService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());

    }

    /**
     *根据id删除
     */
    @RequestMapping("deleteLoginfo")
    public ResultObj deleteLoginfo(Integer id){
        try {
            loginfoService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除日志
     * @param vo
     * @return
     */
    @RequestMapping("batchDeleteLoginfo")
    public ResultObj batchDeleteLoginfo(LoginfoVo vo){
        List<Serializable> idList = new ArrayList<>();
        try {
            for (Integer id : vo.getIds()) {
                idList.add(id);
            }
            loginfoService.removeByIds(idList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
